﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;
using System.IO;

namespace CooStationsToolsNew
{
    class Release
    {
        public Release()
        {
          //  GetList();
        }

        public void StarRelease()
        {
            GetTheList();
            SetName();
            CopyFile(App.appPath + "temp", App.appPath + "Stations//apps//" + App.proName);
            AddProject();
            AddEvent();
            CreatInstallXml();
            GetList();
        }
        public void GetList()
        {
            App.itemList.Clear();
            ArrayList mylist = new ArrayList();
            XmlDocument xml = new XmlDocument();
            xml.Load(App.appPath + "Stations//cfy.xml");

            XmlNodeList list = xml.ChildNodes[1].ChildNodes;

            foreach (XmlNode xn in list)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.Name == "div")
                {
                    string id = xe.GetAttribute("id");
                    int n = id.IndexOf('_');
                    mylist.Add(id.Substring(n + 1));
                }
            }

            App.itemList= mylist;
        }
        ArrayList itemList = new ArrayList();
        private void GetTheList()
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(App.appPath + "Stations//cfy.xml");

            XmlNodeList list = xml.ChildNodes[1].ChildNodes;

            for (int i = 0; i < list.Count; i++)
            {
                XmlElement xe = (XmlElement)list[i];
                if (xe.Name == "div")
                {
                    itemList.Add(xe.GetAttribute("id"));
                }
                if (i == list.Count - 1)
                {
                    y = int.Parse(xe.GetAttribute("y")) + 80;
                }

            }
        }
        int count = 0;
        int y = 100;
        internal string SetItemID()
        {
            int[] s = new int[itemList.Count];
            for (int i = 0; i < itemList.Count; i++)
            {
                string id = itemList[i].ToString();
                try
                {
                    string number = id.Substring(4);
                    int b = int.Parse(number);
                    s[i] = b;
                }
                catch
                {

                }
            }
            fun(1, s);
            return count.ToString();
        }
        public void fun(int num, int[] s)
        {
            int str = num;
            bool tt = false;
            for (int i = 0; i < s.Length; i++)
            {
                if (str == s[i])
                {
                    tt = true;
                    break;
                }
            }
            if (tt)
            {
                num++;
                fun(num, s);
            }
            else
            {
                count = str;
            }
        }

        private void SetName()
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(App.appPath + "Stations//cfy.xml");
            XmlNode xn = xml.ChildNodes[1].ChildNodes[0];
            xn.FirstChild.InnerXml = "<![CDATA[" + App.Cp_Name + "]]>";
            xml.Save(App.appPath + "Stations//cfy.xml");
        }
        private void AddProject()
        {
            string itemid = "item" + SetItemID() + "_" + App.proName;
            XmlDocument doc = new XmlDocument();
            doc.Load(App.appPath + "Stations//cfy.xml");
            XmlNode root = doc.ChildNodes[1];
            XmlElement xediv = doc.CreateElement("div");//创建一个<item>节点 
            xediv.SetAttribute("id", itemid);
            xediv.SetAttribute("x", "23");
            xediv.SetAttribute("y", y.ToString());
            xediv.SetAttribute("alpha", "100");
            root.AppendChild(xediv);

            XmlElement xerect = doc.CreateElement("rect");//创建一个<item>节点 
            xerect.SetAttribute("id", itemid + "bg");
            xerect.SetAttribute("x", "10");
            xerect.SetAttribute("y", "10");
            xerect.SetAttribute("width", "550");
            xerect.SetAttribute("height", "60");
            xerect.SetAttribute("rectAlpha", "100");
            xerect.SetAttribute("rectColor", "0xCCCCCC");
            xerect.SetAttribute("lineColor", "0x999999");
            xerect.SetAttribute("lineAlpha", "100");
            xerect.SetAttribute("border", "2");
            xediv.AppendChild(xerect);

            XmlElement xeText = doc.CreateElement("text");//创建一个<item>节点 
            xeText.SetAttribute("id", itemid + "text");
            xeText.SetAttribute("x", "28");
            xeText.SetAttribute("y", "22");
            xeText.SetAttribute("width", "350");
            xeText.SetAttribute("height", "36");
            xeText.SetAttribute("align", "left");
            xeText.SetAttribute("bold", "false");
            xeText.SetAttribute("size", "25");
            xeText.SetAttribute("color", "0x000000");
            xeText.SetAttribute("multiline", "false");
            xediv.AppendChild(xeText);

            XmlElement xe3 = doc.CreateElement("value");//创建一个<item>节点 
            xe3.InnerXml = "<![CDATA[" + App.proName + "]]>";
            xeText.AppendChild(xe3);

            XmlElement xeinstall = doc.CreateElement("img");//
            xeinstall.SetAttribute("url", "pic/install.jpg");
            xeinstall.SetAttribute("x", "390");
            xeinstall.SetAttribute("y", "20");
            xeinstall.SetAttribute("width", "80");
            xeinstall.SetAttribute("height", "40");
            xeinstall.SetAttribute("onClick", App.proName + "InstallClick");
            xediv.AppendChild(xeinstall);

            XmlElement xego = doc.CreateElement("img");//
            xego.SetAttribute("url", "pic/go.jpg");
            xego.SetAttribute("x", "475");
            xego.SetAttribute("y", "20");
            xego.SetAttribute("width", "80");
            xego.SetAttribute("height", "40");
            xego.SetAttribute("onClick", App.proName + "GoClick");
            xediv.AppendChild(xego);

            doc.Save(App.appPath + "Stations//cfy.xml");
        }

        private void AddEvent()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(App.appPath + "//Stations//cfy_event.xml");
            XmlNode root = doc.ChildNodes[1].ChildNodes[0];

            XmlElement xeItemGoClick = doc.CreateElement("item");//创建一个<item>节点 
            xeItemGoClick.SetAttribute("id", App.proName + "GoClick");
            root.AppendChild(xeItemGoClick);

            XmlElement xestartApp = doc.CreateElement("startApp");//创建一个<item>节点 
            xestartApp.SetAttribute("url", App.Cp_Ip + "/Stations/apps/"+ App.proName+"/");
            xestartApp.SetAttribute("width", App.proWidth);
            xestartApp.SetAttribute("height", App.proHeight);
            xestartApp.SetAttribute("scale", "true");
            xeItemGoClick.AppendChild(xestartApp);

            XmlElement xeInstallClick = doc.CreateElement("item");//创建一个<item>节点 
            xeInstallClick.SetAttribute("id", App.proName + "InstallClick");
            root.AppendChild(xeInstallClick);

            XmlElement xeinstall = doc.CreateElement("install");//创建一个<item>节点
            xeinstall.SetAttribute("url", "install"+ App.proName + ".xml");
            xeInstallClick.AppendChild(xeinstall);

            doc.Save(App.appPath + "Stations//cfy_event.xml");

        }

        private void CreatInstallXml()
        {
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            XmlElement root = doc.CreateElement("app");
            root.SetAttribute("name", "cfy");

            doc.AppendChild(root);

            GetTheFiles(App.appPath + "Stations\\apps\\"+ App.proName, doc, root,true);

            doc.Save(App.appPath + "Stations//install" + App.proName + ".xml");
        }

        private void GetTheFiles(string Path, XmlDocument doc, XmlElement root , bool isroot)
        {
            DirectoryInfo di = new DirectoryInfo(Path);
            if (Directory.Exists(Path))
            {
                foreach (string item in Directory.GetFiles(Path))
                {
                    try
                    {
                        FileInfo f = new FileInfo(item);
                        if (f.Extension.ToLower() != ".db")
                        {
                            string Name = di.Name + "/" + f.Name;
                            XmlElement xe = doc.CreateElement("item");
                            if (isroot)
                            {
                                xe.SetAttribute("path", App.Cp_Ip + "/Stations/apps/" + Name);
                            }
                            else
                            {
                                xe.SetAttribute("path", App.Cp_Ip + "/Stations/apps/" + App.proName +"/" + Name);
                            }
                          
                            root.AppendChild(xe);
                        }
                    }
                    catch { }
                }
                foreach (string item in Directory.GetDirectories(Path))
                {
                    DirectoryInfo dr = new DirectoryInfo(item);
                    if(dr.Name != "Mini")
                    {
                     GetTheFiles(item, doc, root ,false);
                    }
                }
            }
        }

        /// <summary>
        /// 文件夹复制
        /// </summary>
        /// <param name="sourceDirName">原始路径</param>
        /// <param name="destDirName">目标路径</param>
        /// <returns></returns>
        public void CopyFile(string sourceDirName, string destDirName)
        {
            try
            {
                if (sourceDirName.Substring(sourceDirName.Length - 1) != "\\")
                {
                    sourceDirName = sourceDirName + "\\";
                }
                if (destDirName.Substring(destDirName.Length - 1) != "\\")
                {
                    destDirName = destDirName + "\\";
                }
                if (Directory.Exists(sourceDirName))
                {
                    if (!Directory.Exists(destDirName))
                    {
                        Directory.CreateDirectory(destDirName);
                    }
                    foreach (string item in Directory.GetFiles(sourceDirName))
                    {
                        try
                        {
                            File.Copy(item, destDirName + System.IO.Path.GetFileName(item), true);
                        }
                        catch { }
                    }
                    foreach (string item in Directory.GetDirectories(sourceDirName))
                    {
                        CopyFile(item, destDirName + item.Substring(item.LastIndexOf("\\") + 1));
                    }
                }
            }
            catch (Exception)
            {

            }
        }
        public bool Del(string ProName)
        {
            bool flag = false;
            if (DelTheRelease(ProName))
            {
                if (DelEvent(ProName))
                {
                    if (Directory.Exists(App.appPath + "Stations//apps//" + ProName))
                    {
                        Directory.Delete(App.appPath + "Stations//apps//" + ProName, true);
                    }
                    if (File.Exists(App.appPath + "Stations//install" + ProName + ".xml"))
                    {
                        File.Delete(App.appPath + "Stations//install" + ProName + ".xml");
                    }
                    flag= true;
                }
            }
            GetList();
            return flag;
        }

        private bool DelTheRelease(string ProName)
        {
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(App.appPath + "Stations//cfy.xml");
                XmlNodeList list = xml.ChildNodes[1].ChildNodes;
                foreach (XmlNode xn in list)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.Name == "div")
                    {
                        string id = xe.GetAttribute("id");
                        int n = id.IndexOf('_');
                        string IdName = id.Substring(n + 1);
                        if (IdName == ProName)
                        {
                            xml.ChildNodes[1].RemoveChild(xn);
                            break;
                        }
                    }
                }
                xml.Save(App.appPath + "Stations//cfy.xml");
                return true;
            }
            catch
            {
                return false;
            }

        }

        private bool DelEvent(string ProName)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(App.appPath + "//Stations//cfy_event.xml");
                XmlNodeList list = doc.ChildNodes[1].ChildNodes[0].ChildNodes;
                foreach (XmlNode xn in list)
                {
                    XmlElement xe = (XmlElement)xn;
                    string Click = xe.GetAttribute("id");
                    if (Click.Contains(ProName))
                    {
                        doc.ChildNodes[1].ChildNodes[0].RemoveChild(xn);
                    }

                }
                doc.Save(App.appPath + "//Stations//cfy_event.xml");
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
